//
//  OkRegister.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/1/5.
//  Copyright © 2016年 game. All rights reserved.
//

#import "OkRegister.h"
#import "PersonNavView.h"
#import "Manager.h"

@interface OkRegister ()
{
    UIView *_texetView;
    
    UITextField *_textfield;
}
@end

@implementation OkRegister
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.app.myTabBar.tabBar.hidden = YES;
    [self NavView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CrearTextField];
    [self FootView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Tap)];
    [self.view addGestureRecognizer:tap];
}

-(void)Tap{
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];

}
-(void)NavView{
   
    PersonNavView *naview = [[PersonNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    naview.title.text = @"注册";
    naview.BackToFatherView = ^{
        [self.navigationController popViewControllerAnimated:YES];
    };
    [self.view addSubview:naview];
}

-(void)CrearTextField{
    
    _texetView =[self Linview:CGRectMake(-1, 84, SCREEN_WIDTH+2, screen_height*0.3/4) BackgroundColor:[UIColor whiteColor] AddView:self.view];
    _texetView.backgroundColor =  [UIColor whiteColor];
    _texetView.layer.borderWidth = 0.5;
    _texetView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _texetView.layer.masksToBounds = YES;
    
    _textfield= [self CrectTextFieldWhithRect:CGRectMake(20, 4, SCREEN_WIDTH-40, H(_texetView)-5) Placeholder: @"请输入用户名" FatherView:_texetView];
    
}
-(void)FootView{
    
    UIButton *nextBnt = [self CreatBntAddView:self.view WhithRect:CGRectMake(X(_textfield),Height(_texetView)+15, SCREEN_WIDTH-X(_textfield)*2, 35) AndType:UIButtonTypeSystem AndBntImgName:@"uyisdn," AndTitle:@"完成注册" Font:SCREEN_WIDTH*0.05];
    radius(nextBnt, 3);
    nextBnt.backgroundColor = mainColor;
    [nextBnt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBnt addTarget:self action:@selector(finishRegist) forControlEvents:UIControlEventTouchUpInside];
}

-(void)finishRegist{

    if (_textfield.text.length==0) {
        [Manager GetHudWhithText:@"请输入用户名" AndFatherView:self.view];
    }else{
    
        [self ToRegist];
    }
}

-(void)ToRegist{
    
    [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        self.formhash= responseObject[@"data"][@"formhash"];
        
        NSDictionary *dic = @{
                              @"username":[Manager doEncryptStr:_textfield.text withKey:@"diaoyurbbs"],
                              @"password":[Manager doEncryptStr:_passWord withKey:@"diaoyurbbs"],
                              @"password2":[Manager doEncryptStr:_passWord withKey:@"diaoyurbbs"],
                              @"regsubmit":@"yes",
                              @"regmessage":@"我爱钓鱼人",
                              @"formhash":_formhash,
                              @"sms_code":self.testText,
                              @"mobile":self.phoneNumb,
                              };

        [[Manager sharedManager]ResponseWhithURL:RegisterUrl AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
            if (!error) {
                
                if ([responseObject[@"code"] isEqualToString:@"200"]) {//注册成功，直接登录好
                    
                    [Manager GetHudWhithText:@"恭喜您您完成注册" AndFatherView:self.view];
                     NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
                    [ud setObject:[Manager doEncryptStr:_textfield.text withKey:@"diaoyurbbs"] forKey:@"userName"];
                    [ud setObject:[Manager doEncryptStr:_passWord withKey:@"diaoyurbbs"] forKey:@"userPass"];
                    [ud setObject:[NSDate date] forKey:@"time"];
                    [ud synchronize];
                    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
                    app.isLogin = @"YES";
                    [self.navigationController popToRootViewControllerAnimated:YES];
                    
                }else{
                    
                [Manager GetHudWhithText:responseObject[@"message"] AndFatherView:self.view];
                    
                }
            }else{
                [Manager GetHudWhithText:@"注册失败" AndFatherView:self.view];
            }
        }];        
    }];

}


-(UITextField *)CrectTextFieldWhithRect:(CGRect )rect Placeholder:(NSString *)string FatherView:(UIView *)fatherView{
    
    UITextField *textfiled = [[UITextField alloc]initWithFrame:rect];
    textfiled.placeholder = string;
    textfiled.clearButtonMode = UITextFieldViewModeAlways;
    textfiled.font = font(SCREEN_WIDTH*0.05);
    [fatherView addSubview:textfiled];
    
    return textfiled;
}

@end
